package com.ef.service.impl;

import java.util.List;

import com.ef.constant.StatusType;
import com.ef.dao.UserDao;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.service.UserService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class UserServiceImpl extends BaseServiceImpl implements UserService {

	private UserDao userDao;

	public String getPasswordByLoginName(String loginName) {
		User loginUser = userDao.getUser(loginName);
		if (loginUser != null) {
			return loginUser.getPassword();
		} else {
			return "";
		}
	}

	public User getUserByCaller(String caller) {

		return userDao.findByCaller(caller.trim());
	}

	public User getUserByLoginName(String loginName) {

		return userDao.getUser(loginName);
	}

	public PaginationSupport<User> findByExample(CustomExample<User> example,
			int startIndex, int pageSize) {

		return userDao.findPageByExample(example, startIndex, pageSize);
	}

	public void saveOrUpdate(User entity) {
		userDao.clear();
		userDao.saveOrUpdate(entity);
	}

	public void delete(User entity) {
		entity.setStatus(StatusType.INVALID);
		userDao.update(entity);
	}

	public User findById(Long id) {
		return userDao.findById(id);
	}

	public List<User> findAllCc() {
		return userDao.findByRoleName("CC");
	}

	public List<User> findAllCr() {
		// DetachedCriteria detachedCriteria =
		// DetachedCriteria.forClass(User.class);
		// detachedCriteria.add(Restrictions.eq("role.name", "CR"));
		return userDao.findByRoleName("CR");
	}

	public List<User> findAllTm() {

		return userDao.findByRoleName("TM");
	}
	
	public List<User> findAllCm(){
		
		return userDao.findByRoleName("CM");
	}

	public List<User> findAllCcSameSchool(School school) {
		return userDao.findByRoleAndSchool("CC", school);
	}
 
	public List<User> findAllCrSameSchool(School school) {
		return userDao.findByRoleAndSchool("CR", school);
	}

	public User findByWorkNo(String workNo) {

		return userDao.findByWorkNo(workNo);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public List<User> findByAnything(String qitem, int max) {
		StringBuffer hql = new StringBuffer(
				"From User where(lower(name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(loginName) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(workcode) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and status='VALID' ");
		// DetachedCriteria criterion = DetachedCriteria.forClass(User.class);
		// Disjunction or = Restrictions.disjunction();
		// Property myProperty = Property.forName("name");
		// or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
		// or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
		// criterion.add(or);
		// criterion.add(Restrictions.eq("status", StatusType.VALID));
		return userDao.findMultiAll(hql.toString(), 0, max);
	}

	public List<User> findByAnythingWithRolename(String rolename, String qitem,
			int max) {
		StringBuffer hql = new StringBuffer(
				"Select user From User user  inner join user.role as role  where lower(role.name) like '%");
		hql.append(rolename.trim().toLowerCase());
		hql.append("%' and (lower(user.loginName) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(user.workcode) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and user.status='VALID' ");
		return userDao.findMultiAll(hql.toString(), 0, max);
//		DetachedCriteria criterion = DetachedCriteria.forClass(User.class);
//		criterion.createCriteria("role").add( 
//				Restrictions.like("name", rolename, MatchMode.ANYWHERE));
//		Disjunction or = Restrictions.disjunction();
//		Property myProperty = Property.forName("name");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		criterion.add(or);
//		criterion.add(Restrictions.eq("status", StatusType.VALID));
//		return userDao.findAllByCriteria(criterion, max);
	}
}
